// pages/couponsMgt/couponsIssue/index.js
import Common from '../../../common/common.js';
const app = getApp();
let common = new Common;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orgSeq:'',//店铺id
    ipx:'',
    setDate: 9,//默认显示全部
    listArray:[],
    select_all: false,
    batchIds: '',    //选中的ids
    on:1,
    date: '请选择', 
    enddate: '请选择',
    hide:true,
    page:1,
    keyWords:'',//搜索条件
    status: '',
  },
  // 取消
  cancelMask: function () { 
    this.setData({
      hide:true,
      date: '',
      enddate: '' 
    })
  },
  // 确定
   corfimMask: function () {
     this.setData({
       hide: true,
     })
     this.gitUserList();
    },
  showMask: function () {
  
    this.setData({
      hide: false,
      date:'请选择',
      enddate:'请选择' 
    })
  },
  // 开始时间
  bindDateChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date: e.detail.value
    })
  },
  // 结束时间
  bindDateChangeend: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      enddate: e.detail.value
    })
  },
  // 筛选点击绑定关系
  setBdStatus:function(e){
    let status = e.currentTarget.dataset.status;
    this.setData({
      on: status
    })
  },
  selectall: function (e) {
    console.log(e)
    var that = this;
    var arr = [];   //存放选中id的数组
    for (let i = 0; i < that.data.listArray.length; i++) {
      that.data.listArray[i].checked = (!that.data.select_all)
      if (that.data.listArray[i].checked == true) {
        // 全选获取选中的值
        arr = arr.concat(that.data.listArray[i].id.split(','));
      }
    }
    console.log(arr)
    that.setData({
      listArray: that.data.listArray,
      select_all: (!that.data.select_all),
      batchIds: arr
    })
  },

  // 单选
  checkboxChange: function (e) {
    console.log(e)
    console.log(e.detail.value)
    this.setData({
      batchIds: e.detail.value  //单个选中的值
    })
    console.log(this.data.batchIds)
  },
  // 点击选择日期
  setDate:function(e){
    console.log(e)
    this.setData({
      setDate: e.currentTarget.dataset.type,
    })
  },
//  确认派送
  toSureCoupons:function(e){
    var date = this.data.date.replace(/-/g, '/');
    var dateStartTamp = Date.parse(date);
      console.log('开始时间戳=====' + dateStartTamp)
      // 结束时间戳   
    var enddate = this.data.enddate.replace(/-/g, '/');
    var dateEndTamp = Date.parse(enddate);
      if (dateStartTamp > dateEndTamp){
        wx.showToast({
          title:'开始时间不能大于结束时间',
          icon:'none'
        })
        return;
      }
    if (!this.data.batchIds){
      wx.showToast({
        title: '请选择用户',
        icon:'none'
      })
      return;
    }else{
      wx.navigateTo({
        url: "/pages/couponsMgt/couponsIssue/sureCoupons/index?memberList=" + this.data.batchIds,
      })
    }
  },
  getval:function(e){
    this.setData({
      keyWords:e.detail.value
    })
    console.log(this.data.keyWords)
  },
 gitUserList:function(page,type){
   let data = {
     "size": 10,
     "current":this.data.page,
     "condition": {
         "orgSeq": "2e8f4c46-ee3b-4b4b-9270-cf00c28271af",
      //  "orgSeq": this.data.orgSeq,
         keyWords:this.data.keyWords,
       status: this.data.on,
       beginTime: this.data.date, 
       endTime: this.data.enddate
       }
     }
   common.requestAjax('/api/member-service/v1/member/queryMember', 'POST', data).then(res => {
     if (res.data.code == 1) {
       let list = res.data.records;
       if (type == 1) {
         //下拉刷新
         list = res.data.records;
       } else if (type == 2) {
         //上拉加载
         list = list.concat(res.data.records);
       }
       this.setData({
         listArray: list
       })
       wx.hideLoading();
     }

   }).catch(err => {
     console.log(err);
   })
 },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let orgSeq = app.globalData.orgSeq;
    console.log(orgSeq)
    let ipx = app.globalData.isIphoneX;
    this.setData({
      ipx: ipx,
      orgSeq: orgSeq,
    });
    this.gitUserList(1,1)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.gitUserList(1,1);
    setTimeout(() => {
      wx.stopPullDownRefresh();
    }, 1200)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let page = this.data.page;
    page = page+1;
    this.gitUserList(page, 2);
    this.setData({
      page: page
    })
   
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})